/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import java.net.URLEncoder;

import junit.framework.TestCase;

import org.leeloo.databinding.ui.event.EventParser;
import org.leeloo.databinding.ui.test.support.EventParserModel;

public class EventParserTest extends TestCase {
    StringBuffer buffer;

    String stringValue;

    int intValue;

    EventParser parser;

    String className;

    protected void setUp() throws Exception {
        buffer = new StringBuffer();
        stringValue = "stringValue";
        intValue = 1;
        parser = new EventParser();
        className = EventParserModel.class.getName();
    }

    public void testNoClassName() throws Exception {
        try {
            buffer.append("a=b");
            parser.parse(buffer.toString());
            fail("exception should have been thrown");
        } catch (IllegalArgumentException e) {
        }
    }

    public void testCreatesClassFromClassName() throws Exception {
        append(buffer, "className", className);

        Object object = parser.parse(buffer.toString());
        assertNotNull(object);
        assertTrue(object instanceof EventParserModel);
    }

    public void testSetValuesOnRoot() throws Exception {
        append(buffer, "className", className);
        append(buffer, "stringValue", stringValue);
        append(buffer, "intValue", intValue);

        EventParserModel model = (EventParserModel) parser.parse(buffer.toString());
        assertEquals(stringValue, model.getStringValue());
        assertEquals(intValue, model.getIntValue());
    }

    public void testSetValuesOnChild() throws Exception {
        append(buffer, "className", className);
        append(buffer, "parserModel.stringValue", stringValue);
        append(buffer, "parserModel.intValue", intValue);

        EventParserModel model = (EventParserModel) parser.parse(buffer.toString());
        assertNotNull(model.getParserModel());
        model = model.getParserModel();
        assertEquals(stringValue, model.getStringValue());
        assertEquals(intValue, model.getIntValue());
    }
    
    public void testSetValueOnChildOfChild() throws Exception {
        append(buffer, "className", className);
        append(buffer, "parserModel.parserModel.stringValue", stringValue);
        
        EventParserModel model = (EventParserModel) parser.parse(buffer.toString());
        assertNotNull(model.getParserModel().getParserModel());
        assertEquals(stringValue, model.getParserModel().getParserModel().getStringValue());
    }

    public void testDecodeStringValues() throws Exception {
        stringValue = "decode test";
        String encodedString = URLEncoder.encode(stringValue, "UTF-8");

        assertFalse(stringValue.equals(encodedString));
        append(buffer, "className", className);
        append(buffer, "stringValue", encodedString);

        EventParserModel model = (EventParserModel) parser.parse(buffer.toString());
        assertEquals(stringValue, model.getStringValue());
    }

    private void append(StringBuffer buffer, String name, int value) {
        append(buffer, name, Integer.toString(value));
    }

    private void append(StringBuffer buffer, String name, String value) {
        if (buffer.length() > 0) {
            buffer.append("\t");
        }

        buffer.append(name).append("=").append(value);
    }
}
